<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/15 0015
 * Time: 10:08
 */

namespace Cohuna\Models;
use Phalcon\Di;

/**
 * 支付配置表
 * @package Cohuna\Models
 */
class CohunaPay extends ModelBase
{
    public $id;
    public $pay_id;
    public $merchant_id;
    public $pay_name;
    public $pay_account;
    public $pay_platform;
    public $sign_type;
    public $gateway;
    public $pay_status;
    public $pay_config;
    public $is_delete;
    public $remark;
    public $create_time;
    public $create_id;
    public $update_time;
    public $update_id;

    public static function fromArray(array $data)
    {
        $object = new CohunaPay();

        $object->id             = isset($data['id']) ? $data['id'] : null;
        $object->pay_id         = isset($data['pay_id']) ? $data['pay_id'] : null;
        $object->merchant_id    = isset($data['merchant_id']) ? $data['merchant_id'] : null;
        $object->pay_name       = isset($data['pay_name']) ? $data['pay_name'] : null;
        $object->pay_platform   = isset($data['pay_platform']) ? $data['pay_platform'] : null;
        $object->sign_type      = isset($data['sign_type']) ? $data['sign_type'] : null;
        $object->gateway        = isset($data['gateway']) ? $data['gateway'] : null;
        $object->pay_status     = isset($data['pay_status']) ? $data['pay_status'] : null;
        $object->pay_config     = isset($data['pay_config']) ? $data['pay_config'] : null;
        $object->remark         = isset($data['remark']) ? $data['remark'] : null;
        $object->create_time    = isset($data['create_time']) ? $data['create_time'] : null;
        $object->create_id      = isset($data['create_id']) ? $data['create_id'] : 0;
        $object->update_time    = isset($data['update_time']) ? $data['update_time'] : null;
        $object->update_id      = isset($data['update_id']) ? $data['update_id'] : null;

        if(isset($data['pay_config'])){
            if(isset($data['pay_config']['mch_id'])){
                $object->pay_account = $data['pay_config']['mch_id'];
            }elseif (isset($data['pay_config']['alipay_account'])){
                $object->pay_account = $data['pay_config']['alipay_account'];
            }
        }
        return $object;
    }

    public static function createPayId()
    {
       list($id,$str) = explode('.',uniqid('',1));
       return $id . substr($str,0,16-strlen($id));
    }

    public function getSource()
    {
        return Di::getDefault()->getShared('config')->database->default->prefix . 'pay';
    }
    public function beforeUpdate()
    {
        $this->update_time = date('Y-m-d H:i:s');
    }
    public function beforeCreate()
    {
        $this->create_time = date('Y-m-d H:i:s');
        $this->is_delete = 0;
    }

    /**
     * 保存前将数组转换为字符串
     */
    public function beforeSave()
    {
        if(empty($this->pay_config) === false && is_array($this->pay_config)){
            if(empty($this->pay_account) && isset($this->pay_config['mch_id'])){
                $this->pay_account =  $this->pay_config['mch_id'] ;
            }elseif (empty($this->pay_account) && isset($this->pay_config['alipay_account'])){
                $this->pay_account = $this->pay_config['alipay_account'];
            }

            $this->pay_config = json_encode($this->pay_config,JSON_UNESCAPED_UNICODE);
        }
    }

    /**
     * 查询数据后还原配置到数组
     */
    public function afterFetch()
    {
        if(empty($this->pay_config) === false && is_string($this->pay_config)){
            $this->pay_config = json_decode($this->pay_config,true);
        }
    }

    /**
     * 保存之后还原配置到数组
     */
    public function afterSave()
    {
        if(empty($this->pay_config) === false && is_string($this->pay_config)){
            $this->pay_config = json_decode($this->pay_config,true);
        }
    }
}